package com.mdlib.helper.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.ui.widget.CommonTopBar;

import butterknife.ButterKnife;

/**
 * Created by bin on 16/10/21.
 */

public abstract class BaseFragment extends Fragment {

    protected BaseActivity mActivity;

    protected CommonTopBar mTopbar;

    protected void initParam(Bundle args) {

    }

    //获取布局文件ID
    protected abstract int getLayoutId();


    protected abstract void initView(View view, Bundle savedInstanceState);


    //获取宿主Activity
    protected BaseActivity getHoldingActivity() {
        return mActivity;
    }

    private void initTopbar(View view) {
        mTopbar = (CommonTopBar) view.findViewById(R.id.top_bar);
        if (mTopbar != null) {
            view.findViewById(R.id.fl_title_left).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeFragment();
                }
            });
        }
    }

    protected void initTopbar(boolean hasBack, String title) {
        initTopbar(hasBack, title, R.color.white);
    }

    public void initTopbars(boolean hasBack, String title, int leftImageId) {
        if (mTopbar == null) {
            return;
        }

        mTopbar.setVisibility(View.VISIBLE);

        if (hasBack) {
            mTopbar.setShowImgLeft(true);
        } else {
            mTopbar.setShowImgLeft(false);
        }

        mTopbar.setTextCenter(title);

        ImageView leftImage = (ImageView) mTopbar.findViewById(R.id.iv_title_left);
        if (leftImage != null) {
            if (leftImageId == 0) {
                leftImage.setImageResource(R.drawable.general_back_b);
            } else {
                leftImage.setImageResource(leftImageId);
            }
        }

    }

    protected void initTopbar(boolean hasBack, int stringId) {
        initTopbar(hasBack, getResources().getString(stringId), R.color.white);
    }

    protected void initTopbar(boolean hasBack, int stringId, int bgColorId) {
        initTopbar(hasBack, getResources().getString(stringId), bgColorId);
    }


    protected void initTopbar(boolean hasBack, String title, int bgColorId) {
        if (mTopbar == null) {
            return;
        }

        mTopbar.setVisibility(View.VISIBLE);

        if (hasBack) {
            mTopbar.setShowImgLeft(true);
        } else {
            mTopbar.setShowImgLeft(false);
        }

        mTopbar.setTextCenter(title);
        ImageView bgView = (ImageView) mTopbar.findViewById(R.id.iv_title_bg);
        if (bgView != null) {
            if (bgColorId == 0) {
                bgView.setBackgroundColor(mActivity.getResources().getColor(R.color.white));
            } else {
                bgView.setBackgroundColor(mActivity.getResources().getColor(bgColorId));
            }
        }
    }

    protected void setRightTvBtn(String rightTextStr, View.OnClickListener clickListener) {
        if (mTopbar == null) {
            return;
        }
        FrameLayout rigthLayout = (FrameLayout) mTopbar.findViewById(R.id.fl_title_right);
        rigthLayout.setOnClickListener(clickListener);
        TextView rightText = (TextView) mTopbar.findViewById(R.id.tv_title_right);
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(rightTextStr);


    }

    protected void setRightIvBtn(int IvId, View.OnClickListener clickListener) {
        if (mTopbar == null) {
            return;
        }
        FrameLayout rigthLayout = (FrameLayout) mTopbar.findViewById(R.id.fl_title_right);
        rigthLayout.setOnClickListener(clickListener);
        ImageView rightIv = (ImageView) mTopbar.findViewById(R.id.iv_title_right);
        rightIv.setVisibility(View.VISIBLE);
        rightIv.setImageResource(IvId);


    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (BaseActivity) context;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParam(getArguments());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, view);
        initTopbar(view);
        initView(view, savedInstanceState);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (null != fragment) {
            getHoldingActivity().addFragment(fragment);
        }
    }

    //移除fragment
    protected void removeFragment() {
        getHoldingActivity().removeFragment();
    }


}
